package mdm.com.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mdm.com.util.DateUtil;
import mdm.com.web.common.Constant;
import mdm.com.web.dao.PermissionDetailMapper;
import mdm.com.web.dao.PermissionMapper;
import mdm.com.web.dao.RoleMapper;
import mdm.com.web.dao.RolePermissionMapper;
import mdm.com.web.entity.PermissionDetail;
import mdm.com.web.entity.Role;
import mdm.com.web.entity.RolePermission;
import mdm.com.web.service.IRoleService;


@Service("iRoleService")
public class RoleServiceImpl implements IRoleService {
    private final RoleMapper             roleMapper;
    private final RolePermissionMapper   rolePermissionMapper;
    private final PermissionMapper       permissionMapper;
    private final PermissionDetailMapper permissionDetailMapper;

    @Autowired
    public RoleServiceImpl(RoleMapper roleMapper, RolePermissionMapper rolePermissionMapper,
            PermissionMapper permissionMapper, PermissionDetailMapper permissionDetailMapper) {
        this.roleMapper = roleMapper;
        this.rolePermissionMapper = rolePermissionMapper;
        this.permissionMapper = permissionMapper;
        this.permissionDetailMapper = permissionDetailMapper;
    }

    @Override
    public Role selectByRoleName(String roleName) throws Exception {
        return roleMapper.selectByRoleName(roleName);
    }

    @Override
    public void add(Role role) throws Exception {
        role.setCreateTime(DateUtil.formatDateTime(new Date()));
        role.setState(Constant.STATE_EFFECTIVE);
        Role roleName = roleMapper.selectByRoleName(role.getcRoleName());
        if (roleName == null) {
            roleMapper.insertSelective(role);
            System.out.println("=======roleid======" + role.getcRoleId());
        }

        // List<Permission> basePermissions =
        // permissionMapper.selectBasePermission();
        // Set<Integer> permissionIds = role.getcPermissionIds();
        // for (Permission permission : basePermissions) {
        // permissionIds.add(permission.getcPermissionId());
        // }

        // Set<Integer> permissionIds = role.getcPermissionIds();
        Set<Integer> detailIds = role.getcDetailIds();

        // for (Integer permissionId : permissionIds) {
        List<PermissionDetail> list = new ArrayList<PermissionDetail>();
        RolePermission rolePermission = null;
        PermissionDetail permissionDetail = null;

        for (Integer detailId : detailIds) {
            System.out.println("======detailId=========" + detailId);
            PermissionDetail permissionDetailByPrimaryKey = permissionDetailMapper.selectByPrimaryKey(detailId);
            rolePermission = new RolePermission();
            rolePermission.setcRoleId(role.getcRoleId());
            rolePermission.setcPermissionId(permissionDetailByPrimaryKey.getcPermissionId());
            rolePermission.setcDetailId(detailId);
            //
            // permissionDetail = new PermissionDetail();
            // permissionDetail.setcDetailId(detailId);
            // list.add(permissionDetail);
            // rolePermission.setcPermissionDetails(list);
            // if (permissionId ==
            // permissionDetailByPrimaryKey.getcPermissionId()) {
            rolePermissionMapper.insertSelective(rolePermission);
            // }
        }
        // }

    }

    @Override
    public void remove(Integer roleId) throws Exception {
        roleMapper.removeByRoleId(roleId);
    }

    @Override
    public void modify(Role role) throws Exception {
        role.setUpdateTime(DateUtil.formatDateTime(new Date()));
        roleMapper.updateByPrimaryKeySelective(role);

        rolePermissionMapper.deleteBycRoleId(role.getcRoleId());
        // List<Permission> basePermissions =
        // permissionMapper.selectBasePermission();
        // Set<Integer> permissionIds = role.getcPermissionIds();
        // for (Permission permission : basePermissions) {
        // permissionIds.add(permission.getcPermissionId());
        // }
        Set<Integer> permissionIds = role.getcPermissionIds();
        Set<Integer> detailIds = role.getcDetailIds();

        List<PermissionDetail> list = new ArrayList<PermissionDetail>();
        RolePermission rolePermission = null;
        PermissionDetail permissionDetail = null;

        for (Integer detailId : detailIds) {
            System.out.println("======detailId=========" + detailId);
            PermissionDetail permissionDetailByPrimaryKey = permissionDetailMapper.selectByPrimaryKey(detailId);
            rolePermission = new RolePermission();
            rolePermission.setcRoleId(role.getcRoleId());
            rolePermission.setcPermissionId(permissionDetailByPrimaryKey.getcPermissionId());
            rolePermission.setcDetailId(detailId);
            //
            // permissionDetail = new PermissionDetail();
            // permissionDetail.setcDetailId(detailId);
            // list.add(permissionDetail);
            // rolePermission.setcPermissionDetails(list);
            // if (permissionId ==
            // permissionDetailByPrimaryKey.getcPermissionId()) {
            rolePermissionMapper.insertSelective(rolePermission);
            // }
        }

        // for (Integer permissionId : permissionIds) {
        // List<PermissionDetail> list = new ArrayList<PermissionDetail>();
        // RolePermission rolePermission = null;
        // PermissionDetail permissionDetail = null;
        //
        // for (Integer detailId : detailIds) {
        // PermissionDetail permissionDetailByPrimaryKey =
        // permissionDetailMapper.selectByPrimaryKey(detailId);
        // rolePermission = new RolePermission();
        // rolePermission.setcRoleId(role.getcRoleId());
        // rolePermission.setcPermissionId(permissionId);
        //
        // permissionDetail = new PermissionDetail();
        // permissionDetail.setcDetailId(detailId);
        // list.add(permissionDetail);
        // rolePermission.setcPermissionDetails(list);
        // if (permissionId == permissionDetailByPrimaryKey.getcPermissionId())
        // {
        // rolePermissionMapper.insertSelective(rolePermission);
        // }
        // }
        // }

        // for (Integer permissionId : permissionIds) {
        // RolePermission rolePermission = new RolePermission();
        // rolePermission.setcPermissionId(permissionId);
        // rolePermission.setcRoleId(role.getcRoleId());
        // rolePermissionMapper.insertSelective(rolePermission);
        // }
    }

    @Override
    public List<Role> selectByCondition(Role role) throws Exception {
        if (StringUtils.isNotEmpty(role.getEndDate())) {
            role.setEndDate(DateUtil.addDate(role.getEndDate(), 1));
        }
        return roleMapper.selectByCondition(role);
    }

    // add-jlq-2018-3-13
    @Override
    public List<RolePermission> selectByRoleId(Integer cRoleId) {
        return rolePermissionMapper.selectByRoleId(cRoleId);
    }
}
